USE [DSRSystem_Temp]
GO

/****** Object:  StoredProcedure [dbo].[spGetIndicatorsByCompanyIds]    Script Date: 1/26/2016 12:55:38 PM ******/
DROP PROCEDURE [dbo].[spGetIndicatorsByCompanyIds]
GO

/****** Object:  StoredProcedure [dbo].[spGetIndicatorsByCompanyIds]    Script Date: 1/26/2016 12:55:38 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[spGetIndicatorsByCompanyIds]
	@CompanyIds NVARCHAR(MAX)
AS
BEGIN
	SET NOCOUNT ON;

	DECLARE @XML XML
	SET @XML = CONVERT(XML, @CompanyIds, 1)

	DECLARE @SelectedCompanies TABLE
	(
		Id uniqueidentifier
	)
	INSERT INTO @SelectedCompanies
	SELECT TID.id.value('.','uniqueidentifier') AS Id
	FROM @XML.nodes('/CompanyIds') AS T(c) outer apply T.c.nodes('Id') as TID(id)

	SELECT Indicators.[Id]
			,Indicators.[CompanyId]
			,Indicators.[TemplateId]
			,Indicators.[Number]
			,Indicators.[AnswerCategoryCode]
			,Indicators.[AnswerCategoryText]
			,Indicators.[AnswerCategoryScore]
	FROM [dbo].Indicators AS Indicators
	INNER JOIN @SelectedCompanies AS SelectedCompanies ON SelectedCompanies.Id = Indicators.CompanyId
END

GO


